<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 | 商城系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="icon" type="image/x-icon" href="/favicon.ico"/>
    <style>
        .bg-auth {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        .input-focus:focus {
            box-shadow: 0 0 0 3px rgba(118, 75, 162, 0.2);
        }

        .shake {
            animation: shake 0.5s;
        }

        @keyframes shake {
            0%, 100% {
                transform: translateX(0);
            }
            20%, 60% {
                transform: translateX(-5px);
            }
            40%, 80% {
                transform: translateX(5px);
            }
        }

        .role-switch {
            position: relative;
            display: inline-block;
            width: 220px;
            height: 44px;
        }

        .role-switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .role-slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #e2e8f0;
            transition: .4s;
            border-radius: 22px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .role-slider:before {
            position: absolute;
            content: "";
            height: 36px;
            width: 108px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
            border-radius: 18px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }

        input:checked + .role-slider {
            background-color: #8b5cf6;
        }

        input:checked + .role-slider:before {
            transform: translateX(104px);
        }

        .role-label {
            position: absolute;
            width: 50%;
            text-align: center;
            line-height: 44px;
            font-weight: 500;
            font-size: 14px;
            color: #64748b;
            z-index: 1;
            transition: color 0.3s;
        }

        .role-label.left {
            left: 0;
        }

        .role-label.right {
            right: 0;
        }

        input:checked ~ .role-label.left {
            color: white;
        }

        input:not(:checked) ~ .role-label.right {
            color: white;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
<div class="max-w-md w-full mx-auto">
    <!-- Logo Section -->
    <div class="text-center mb-8">
        <div class="mx-auto w-16 h-16 bg-white rounded-full flex items-center justify-center shadow-lg mb-4">
            <i class="fas fa-lock text-purple-600 text-2xl"></i>
        </div>
        <h1 class="text-3xl font-bold text-gray-800">欢迎回来</h1>
        <p class="text-gray-600 mt-2">登录您的账户继续</p>
    </div>

    <!-- Login Card -->
    <div class="bg-white rounded-xl shadow-xl overflow-hidden">
        <div class="p-8">
            <!-- Role Switch -->
            <div class="flex justify-center mb-6">
                <label class="role-switch">
                    <input type="checkbox" id="roleToggle" name="role">
                    <span class="role-slider"></span>
                    <span class="role-label left">顾客</span>
                    <span class="role-label right">商家</span>
                </label>
            </div>

            <!-- Error Message (Thymeleaf) -->
            <div th:if="${error}" class="mb-6 bg-red-50 border-l-4 border-red-500 p-4">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-circle text-red-500"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-red-700">
                            用户名或密码错误，请重试。
                        </p>
                    </div>
                </div>
            </div>

            <!-- Logout Message (Thymeleaf) -->
            <div th:if="${logout}" class="mb-6 bg-green-50 border-l-4 border-green-500 p-4">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <i class="fas fa-check-circle text-green-500"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-green-700">
                            您已成功登出。
                        </p>
                    </div>
                </div>
            </div>

            <form th:action="@{/doLogin}" method="post" id="loginForm" class="space-y-6" th:object="${loginForm}">
                <!-- Hidden role field -->
                <input type="hidden" id="role" name="role" th:field="*{role}">

                <!-- Username/Email Field -->
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700 mb-1">邮箱或用户名</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fas fa-user text-gray-400"></i>
                        </div>
                        <input type="text" id="username" name="username"
                               class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-lg input-focus focus:border-purple-500 focus:outline-none transition duration-150"
                               placeholder="请输入您的邮箱或用户名"
                               required
                               th:field="*{username}">
                    </div>
                    <p id="usernameError" class="mt-1 text-sm text-red-600 hidden">请输入有效的邮箱或用户名</p>
                </div>

                <!-- Password Field -->
                <div>
                    <div class="flex justify-between items-center mb-1">
                        <label for="password" class="block text-sm font-medium text-gray-700">密码</label>
                        <a href="#" class="text-sm font-medium text-purple-600 hover:text-purple-500">忘记密码?</a>
                    </div>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fas fa-key text-gray-400"></i>
                        </div>
                        <input type="password" id="password" name="password"
                               class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-lg input-focus focus:border-purple-500 focus:outline-none transition duration-150"
                               placeholder="请输入您的密码"
                               required
                               th:field="*{password}">
                        <button type="button" id="togglePassword"
                                class="absolute inset-y-0 right-0 pr-3 flex items-center">
                            <i class="fas fa-eye text-gray-400 hover:text-gray-600"></i>
                        </button>
                    </div>
                    <p id="passwordError" class="mt-1 text-sm text-red-600 hidden">密码长度至少为6位</p>
                </div>

                <!-- Remember Me -->
                <div class="flex items-center">
                    <input id="remember-me" name="remember-me" type="checkbox"
                           class="h-4 w-4 text-purple-600 focus:ring-purple-500 border-gray-300 rounded">
                    <label for="remember-me" class="ml-2 block text-sm text-gray-700">
                        记住我
                    </label>
                </div>

                <!-- Submit Button -->
                <div>
                    <button type="submit" id="submitBtn"
                            class="w-full flex justify-center py-3 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 transition duration-150">
                        登录
                        <i class="fas fa-arrow-right ml-2"></i>
                    </button>
                </div>
            </form>
        </div>

        <!-- Footer/Sign Up Link -->
        <div class="bg-gray-50 px-8 py-4 text-center">
            <p class="text-sm text-gray-600">
                还没有账户?
                <a href="/register" class="font-medium text-purple-600 hover:text-purple-500">立即注册</a>
            </p>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const loginForm = document.getElementById('loginForm');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const togglePassword = document.getElementById('togglePassword');
        const usernameError = document.getElementById('usernameError');
        const passwordError = document.getElementById('passwordError');
        const submitBtn = document.getElementById('submitBtn');
        const roleToggle = document.getElementById('roleToggle');
        const roleField = document.getElementById('role');

        // Toggle password visibility
        togglePassword.addEventListener('click', function () {
            const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
            passwordInput.setAttribute('type', type);
            this.innerHTML = type === 'password' ? '<i class="fas fa-eye text-gray-400 hover:text-gray-600"></i>' :
                '<i class="fas fa-eye-slash text-gray-400 hover:text-gray-600"></i>';
        });

        // Handle role toggle
        roleToggle.addEventListener('change', function () {
            if (this.checked) {
                roleField.value = 'merchant';
            } else {
                roleField.value = 'customer';
            }
        });

        // Form validation
        loginForm.addEventListener('submit', function (e) {
            let isValid = true;

            // Validate username
            if (usernameInput.value.trim() === '') {
                usernameError.classList.remove('hidden');
                usernameInput.classList.add('border-red-500');
                usernameInput.classList.remove('border-gray-300');
                isValid = false;
            } else {
                usernameError.classList.add('hidden');
                usernameInput.classList.remove('border-red-500');
                usernameInput.classList.add('border-gray-300');
            }

            // Validate password
            if (passwordInput.value.length < 6) {
                passwordError.classList.remove('hidden');
                passwordInput.classList.add('border-red-500');
                passwordInput.classList.remove('border-gray-300');
                isValid = false;
            } else {
                passwordError.classList.add('hidden');
                passwordInput.classList.remove('border-red-500');
                passwordInput.classList.add('border-gray-300');
            }

            if (!isValid) {
                e.preventDefault();
                loginForm.classList.add('shake');
                setTimeout(() => {
                    loginForm.classList.remove('shake');
                }, 500);

                // Change button to show error
                submitBtn.innerHTML = '<i class="fas fa-exclamation-circle mr-2"></i> 请修正错误';
                submitBtn.classList.remove('bg-purple-600', 'hover:bg-purple-700');
                submitBtn.classList.add('bg-red-500', 'hover:bg-red-600');

                setTimeout(() => {
                    submitBtn.innerHTML = '登录 <i class="fas fa-arrow-right ml-2"></i>';
                    submitBtn.classList.remove('bg-red-500', 'hover:bg-red-600');
                    submitBtn.classList.add('bg-purple-600', 'hover:bg-purple-700');
                }, 2000);
            }
        });

        // Real-time validation for better UX
        usernameInput.addEventListener('input', function () {
            if (this.value.trim() !== '') {
                usernameError.classList.add('hidden');
                this.classList.remove('border-red-500');
                this.classList.add('border-gray-300');
            }
        });

        passwordInput.addEventListener('input', function () {
            if (this.value.length >= 6) {
                passwordError.classList.add('hidden');
                this.classList.remove('border-red-500');
                this.classList.add('border-gray-300');
            }
        });
    });
</script>
</body>
</html>